package com.be.beadmin.device.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.InspectFileEx;
import com.be.beadmin.project.Util.MinioUtil;
import com.be.beadmin.service.impl.MinioService;
import io.minio.Result;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.InspectFileService;
import com.be.beadmin.device.dto.input.InspectFileQueryCriteria;
import com.be.beadmin.device.dto.input.InspectFileDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author zty
* @date 2023-10-12
**/
@RestController
public class InspectFileController {

    @Autowired
    private  InspectFileService inspectFileService;

    @Autowired
    private MinioService minioService;

    MinioUtil minioUtil = new MinioUtil();

    private String bucketName = "inspectfile";

    @PostMapping(value = "/device/getInspectFileByPage")
    public ResultModel getInspectFileByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectFileService.getInspectFileByPage(pageEntity)) ;
    }

    @PostMapping("/device/addInspectFile")
    public ResultModel addInspectFile(@RequestBody InspectFileDto inspectFileDto){
        inspectFileService.addInspectFile(inspectFileDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectFile")
    public ResultModel delInspectFile(@RequestBody InspectFileDto inspectFileDto){
        inspectFileService.delInspectFile(inspectFileDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectFile")
    public ResultModel updateInspectFile(@RequestBody InspectFileDto inspectFileDto){
        inspectFileService.updateInspectFile(inspectFileDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportInspectFile")
    public void exportInspectFile(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectFileEx> inspectFilePage = inspectFileService.getInspectFileByPage(pageEntity);
        List<InspectFileEx> inspectFileExes = inspectFilePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectFile接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), InspectFileEx.class).sheet("sheet1").doWrite(inspectFileExes);
    }

    @ApiOperation("新增附件信息")
    @PostMapping("/device/uploadInspectFile")
    public ResultModel uploadInspectFile(
            @ApiParam(value = "附件",required = true) @RequestParam("file") MultipartFile[] multipartFiles,
            @ApiParam(value = "einspectitem_id",required = true) @RequestParam String inspectitemId,
            @ApiParam(value = "文件名称") @RequestParam String fileName,
            @ApiParam(value = "备注") @RequestParam(required = false) String memo) throws Exception {

        inspectFileService.uploadInspectFile(inspectitemId,multipartFiles,fileName,memo,bucketName);
        return ResultModel.success("添加附件成功");

    }


    @ApiOperation("巡检附件预览")
    @PostMapping("/device/previewInspectFile")
    public ResultModel previewInspectFile(@ApiParam(value = "附件id",required = true) @RequestParam String id)throws Exception {
        return ResultModel.success(inspectFileService.previewInspectFile(id));

    }

    @ApiOperation("附件下载")
    @PostMapping("/device/downloadInspectFile")
    public ResultModel downloadInspectFile(
            @ApiParam(value = "附件id",required = true) @RequestParam String id,
            HttpServletResponse response) throws Exception{
        inspectFileService.downloadInspectFile(id,response);
        return null;
    }
}